package cst.a931xmc.yuejian20.Activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import java.util.List;

import cst.a931xmc.yuejian20.Adapter.ClassAdapter;
import cst.a931xmc.yuejian20.Adapter.VideoAdapter;
import cst.a931xmc.yuejian20.Bean.ClassBean;
import cst.a931xmc.yuejian20.Bean.ClubBean;
import cst.a931xmc.yuejian20.Bean.MasterBean;
import cst.a931xmc.yuejian20.Bean.VideoBean;
import cst.a931xmc.yuejian20.Listener.Listener;
import cst.a931xmc.yuejian20.Model.ApiModel;
import cst.a931xmc.yuejian20.Model.ClassModel;
import cst.a931xmc.yuejian20.Model.ClubModel;
import cst.a931xmc.yuejian20.Model.MasterModel;
import cst.a931xmc.yuejian20.R;

public class ClubActivity extends AppCompatActivity implements View.OnClickListener {
    private Context context;
    private int club_id;
    private int master_id;
    private ImageView club_pic;
    private TextView club_name;
    private TextView club_place;
    private TextView club_time;
    private TextView club_tel;
    private TextView club_intro;
    private TextView master_name;
    private ImageView master_pic;
    private ImageButton btn_back;
    private ClubModel model;
    private Listener<ClubBean> listener;
    private MasterModel model1;
    private Listener<MasterBean> listener1;
    private ClassModel model2;
    private Listener<List<ClassBean>> listener2;
    private Listener<List<VideoBean>> listener3;
    private RecyclerView recyclerView1,recyclerView2;
    private ClassAdapter adapter;
    private VideoAdapter adapter1;
    private GridLayoutManager layoutManager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_club);
        init();
        initData();
        model = new ClubModel();
        model.getDetail(club_id,listener);
        initViews();
    }

    public void init(){
        club_pic = findViewById(R.id.club_pic);
        club_name = findViewById(R.id.club_name);
        club_place = findViewById(R.id.club_place);
        club_time = findViewById(R.id.club_time);
        club_tel = findViewById(R.id.club_tel);
        club_intro = findViewById(R.id.club_intro);
        master_name = findViewById(R.id.master_name);
        master_pic = findViewById(R.id.master_pic);
        btn_back = findViewById(R.id.btn_back);
        btn_back.setOnClickListener(this);
        Intent intent = getIntent();
        club_id = intent.getExtras().getInt("club_id");
    }

    public void initData(){
        listener = new Listener<ClubBean>() {
            @Override
            public void onResponse(ClubBean l) {
                if(l.getClub_id()!=0){
                    club_name.setText(l.getClub_name());
                    club_place.setText("地点：" + l.getClub_place());
                    club_time.setText("活动时间：" + l.getClub_time());
                    club_tel.setText("联系电话：" + l.getClub_tel());
                    club_intro.setText(l.getClub_intro());
                    master_id=l.getMaster_id();
                    getMasterInfor();
                    ApiModel apiModel = new ApiModel();
                    Picasso.with(context)
                            .load(apiModel.getBaseUrl() + "static/images/" + l.getClub_pic())
                            .placeholder(R.drawable.nopic)
                            .into(club_pic);
                }
                else {
                    Toast.makeText(ClubActivity.this,"加载失败", Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFail(String message) {
                Toast.makeText(ClubActivity.this,message, Toast.LENGTH_LONG).show();
            }
        };
    }

    public void getMasterInfor(){
        listener1 = new Listener<MasterBean>() {
            @Override
            public void onResponse(MasterBean l) {
                master_name.setText(l.getMaster_name());
                ApiModel apiModel = new ApiModel();
                Picasso.with(context)
                        .load(apiModel.getBaseUrl() + "static/images/" + l.getMaster_pic())
                        .placeholder(R.drawable.nopic)
                        .into(master_pic);
            }
            @Override
            public void onFail(String message) {
                Toast.makeText(ClubActivity.this,message, Toast.LENGTH_LONG).show();
            }
        };
        model1 = new MasterModel();
        model1.getDetail(master_id,listener1);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_back:
                finish();
                break;
        }
    }

    public void initViews(){
        recyclerView1 = findViewById(R.id.rv_class);
        recyclerView1.setLayoutManager(new LinearLayoutManager(ClubActivity.this));
        adapter = new ClassAdapter(ClubActivity.this);
        listener2 = new Listener<List<ClassBean>>() {
            @Override
            public void onResponse(List<ClassBean> list) {
                recyclerView1.setAdapter(adapter);
                adapter.setList(list);
            }

            @Override
            public void onFail(String message) {

            }
        };
        model2 = new ClassModel();
        model2.getClassListByClub(club_id,listener2);
        recyclerView2 = findViewById(R.id.rv_video);
        layoutManager = new GridLayoutManager(context,2);
        recyclerView2.setLayoutManager(new LinearLayoutManager(ClubActivity.this));
        adapter1 = new VideoAdapter(ClubActivity.this);
        listener3 = new Listener<List<VideoBean>>() {
            @Override
            public void onResponse(List<VideoBean> list) {
                layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                    @Override
                    public int getSpanSize(int position) {
                        return 1;
                    }
                });
                recyclerView2.setAdapter(adapter1);
                adapter1.setList(list);
            }

            @Override
            public void onFail(String message) {

            }
        };
        model.getVideoList(club_id,listener3);
    }
}
